<template>
  <!-- 驾驶员管理 -->
  <base-breadcrumb :title="$route.meta.title">
    <!-- 搜索 -->
    <searchData ref="searchData" :searchData="searchArr" @search="search"></searchData>
    <!-- 表格 -->
    <a-card :bordered="false">
      <!-- <title-name :title="$route.meta.title"></title-name> -->
      <!-- 项目部办公室主任、分公司/公司车辆管理员：新增驾驶员信息 -->
      <!-- v-if="post.code === 'PDAZR' || post.code === 'BCACLGL' || post.code === 'COACLGL'" -->
      
      <!-- 表格 -->
      <list-table
        ref="table"
        row-key="id"
        :columns="columns"
        :request="loadData"
        :alert="true"
        show-size-changer
        show-quick-jumper
      >
        <a-button slot="prefix" class="mb12" icon="plus" type="primary" @click="handleManage(2)">新增驾驶员</a-button>
        <span slot="index" slot-scope="text, record, index">{{ index + 1 }}</span>
        <template slot="action" slot-scope="text, record">
          <!-- 项目部办公室主任、分公司/公司车辆管理员：编辑、查看本单位的驾驶员信息 -->
          <!-- (post.code === 'PDAZR' || post.code === 'BCACLGL' || post.code === 'COACLGL') -->
          <a @click="handleManage(0, record)">查看</a>
          <template v-if="record.unitId === post.id">
            <!-- 编辑 -->
            <a-divider type="vertical"></a-divider>
            <a @click="handleManage(1, record)">编辑</a>
          </template>
        </template>
        <!-- 状态 -->
        <template slot="stateName" slot-scope="text">
          <span :class="{ del_btn: text == '不可用' }">{{ text }}</span>
        </template>
      </list-table>
    </a-card>
  </base-breadcrumb>
</template>

<script>
// import { STable } from '@/components' // 表格
import { carDriverInfo } from '@/api/office/vehicle/config' // api
import SelectCompanyAndProject from '@/components/Select/SelectCompanyAndProject'
import BasePage from '@/views/basePage' // 基础配置
import OrgTreeSelect from '@/components/OrgTreeSelect'
import Selector from '@/components/Select/Selector' // 枚举

const searchArr = [
  // 搜索框
  {
    name: '所属单位',
    valueKey: 'searchQuery',
    type: OrgTreeSelect,
    attr: {
      isAutoSelectFirst: true
    }
  },
  // {
  //   name: '所属单位',
  //   valueKey: 'unitId',
  //   type: SelectCompanyAndProject
  // },
  {
    name: '司机类别',
    type: Selector,
    valueKey: 'driverTypeId',
    selectType: '13',
    valueName: 'code',
    urlType: 'union'
  },
  {
    name: '驾驶员姓名',
    type: 'input',
    value: 'driverName'
  },
  {
    name: '入职时间',
    type: 'dateRange',
    value: 'time',
    attr: {
      rangeFormat: ['inJobStartTime', 'inJobEndTime']
    }
  }
]
const columns = [
  {
    title: '序号',
    width: 80,
    dataIndex: 'index',
    scopedSlots: { customRender: 'index' }
  },
  {
    title: '所属单位',
    ellipsis: true,
    dataIndex: 'unitName'
  },
  {
    title: '司机类别',
    ellipsis: true,
    dataIndex: 'driverTypeName'
  },
  {
    title: '姓名',
    ellipsis: true,
    dataIndex: 'driverName'
  },
  {
    title: '性别',
    ellipsis: true,
    dataIndex: 'sex',
    customRender() {
      return ['女', '男'][arguments[0]]
    }
  },
  {
    title: '手机号',
    ellipsis: true,
    dataIndex: 'phone'
  },
  {
    title: '入职时间',
    ellipsis: true,
    dataIndex: 'inJobTime'
  },
  {
    title: '现岗位星级',
    ellipsis: true,
    dataIndex: 'postStarName',
    customRender() {
      return arguments[1]?.driverTypeId !== 'ZN' ? arguments[0] : '—'
    }
  },
  {
    title: '现岗位薪资标准(元)',
    ellipsis: true,
    width: 180,
    dataIndex: 'salary',
    customRender() {
      return arguments[1]?.driverTypeId !== 'ZN' ? arguments[0] : '—'
    }
  },
  {
    title: '满意度平均得分(90分)',
    ellipsis: true,
    width: 180,
    dataIndex: 'satisfactionAvgScore',
    customRender() {
      return arguments[0] || '—'
    }
  },
  {
    title: '日常考核平均得分(100分)',
    ellipsis: true,
    width: 200,
    dataIndex: 'assessAvgScore',
    customRender() {
      return arguments[0] || '—'
    }
  },
  {
    title: '状态',
    dataIndex: 'stateName',
    // fixed: 'right',
    align: 'center',
    scopedSlots: { customRender: 'stateName' }
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: 150,
    align: 'center',
    fixed: 'right',
    scopedSlots: { customRender: 'action' }
  }
]
export default {
  name: 'a' + Date.now(),
  extends: new BasePage(),
  components: {
    // STable
  },
  data() {
    this.columns = columns
    this.searchArr = searchArr
    return {
      queryParam: {}, // 查询参数
      loadData: (parameter) => {
        // if (!this.init) {
        //   return false
        // }
        const requestParameters = Object.assign({}, parameter, { query: this.queryParam })
        return carDriverInfo.getList(requestParameters).then((res) => {
          return res
        })
      }, // 加载数据方法 必须为 Promise 对象
      removeApi: carDriverInfo, // api
      init: false,
      post: {
        code: '', // 登录人组织code
        orgId: '' // 登录人组织id
      }
    }
  },
  mounted() {
    this.post = this.$currentPost()
    this.$refs.searchData.form.setFieldsValue({
      unitId: this.post?.id,
      searchQuery: {
        _checked: this.$currentPost()?.id
      }
    })
    this.init = true
    this.search({
      unitId: this.post?.id,
      searchQuery: {
        _checked: this.$currentPost()?.id
      }
    })
  },
  methods: {
    /**
     * 搜索
     */
    search(value) {
      value && (this.queryParam = value)
      this.$refs.table.refresh(true)
    },
    /**
     * 操作栏
     */
    handleManage(type, record) {
      let query = {
        editModel: type
      }
      if (record?.id) {
        query.id = record.id
      }
      if (record?.driverId) {
        query.driverId = record.driverId
      }
      this.$router.push({
        name: '/vehicle/driverManagementEdit',
        query
      })
    }
  }
}
</script>
